package com.slkj.shilixiaoyuanapp.ui.teacher.tool.homework;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.ToolWorkListEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.view.CustomStateText;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.MySwipeRefreshLayout;
import com.slkj.shilixiaoyuanapp.view.StateLayout;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindColor;
import butterknife.BindView;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.PAGE_COUNT;

/**
 * 历史记录列表
 */
@ActivityInfo(layout = R.layout.activity_task_history_list, title = "历史记录")
public class WorkHistoryListActivity extends BaseActivity {
    @BindView(R.id.recycer)
    RecyclerView recycerType;
    BaseQuickAdapter adapter;
    @BindView(R.id.layout_root)
    StateLayout stateLayout;
    @BindView(R.id.swipeLayout)
    MySwipeRefreshLayout refreshLayout;
    private int page = 1;
    private int count = PAGE_COUNT;
    List<ToolWorkListEntity> toolWorkListEntityList = new ArrayList<>();
    @BindColor(R.color.color_ccc)int colorCcc;
    @BindColor(R.color.colorAccent)int colorAccent;
    @Override
    protected void init() {
        recycerType.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        recycerType.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseQuickAdapter<ToolWorkListEntity, BaseViewHolder>(R.layout.item_main_task_history, toolWorkListEntityList) {
            @Override
            protected void convert(BaseViewHolder helper, final ToolWorkListEntity item) {
                CustomStateText customStateText = helper.getView(R.id.tv_tx);

                helper.setText(R.id.tv_tx,  item.getShows());

                customStateText.setStrokeColor(!item.isCanTx()?colorCcc:colorAccent);
                customStateText.setTextColor((!item.isCanTx()?colorCcc:colorAccent));

                helper.setText(R.id.tv_type, item.getHType());
                helper.setText(R.id.tv_bj, item.getClassName());
                helper.setText(R.id.tv_time, "截止"+item.getEndTime());
//                helper.setText(R.id.tv_tx,  item.isUp() ? "已交齐" : item.getShows());
//                helper.setText(R.id.tv_tx,item.isUrge()?"已催交":item.getShows());
                helper.setOnClickListener(R.id.tv_tx, v -> {
                    if (item.isCanTx()){
                        HttpHeper.get().toolService().cjtx(item.getHId())
                                .compose(bindToLifecycle())
                                .compose(getThread())
                                .subscribe(new CommonCallBack<String>(true, WorkHistoryListActivity.this) {
                                    @Override
                                    public void onCallBackSuccess(String data) {
                                        LoadSuccessAndFailDialog.showSuccess(WorkHistoryListActivity.this,data);
                                        item.setShows(data);
                                        item.setUrge(1);
                                        notifyItemChanged(helper.getAdapterPosition());
                                    }
                                });
                    }
                });
                helper.itemView.setOnClickListener(v -> {
                    WorkDetailsActivity__JumpCenter.builder(WorkHistoryListActivity.this).setTaskId(item.getHId()).create().go();

                });
            }
        };

        adapter.setOnLoadMoreListener(this::loadMore,recycerType);
        recycerType.setAdapter(adapter);
        refresh();
        refreshLayout.setOnRefreshListener(this::refresh);
        stateLayout.setOnReLoadListener(() -> refresh());
    }



    /**
     * 加载更多
     */
    private void loadMore() {
        HttpHeper.get().toolService()
                .getHomeWorkHistory(page, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<ToolWorkListEntity>>( ) {
                    @Override
                    public void onCallBackSuccess(List<ToolWorkListEntity> data) {
                        setData(false,data);
                    }

                    @Override
                    public void onFail(String mes) {
                        adapter.loadMoreFail();
                    }
                });
    }

    /**
     * 刷新
     */
    public void refresh() {
        page = 1;
        //这里的作用是防止下拉刷新的时候还可以上拉加载
        adapter.setEnableLoadMore(false);
        HttpHeper.get().toolService()
                .getHomeWorkHistory(page, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<ToolWorkListEntity>>(stateLayout) {
                    @Override
                    public void onCallBackSuccess(List<ToolWorkListEntity> data) {
                        if (data.isEmpty()){
                            stateLayout.showEmptyView();
                        }
                        setData(true,data);
                        refreshLayout.setRefreshing(false);
                    }
                    @Override
                    public void onFail(String mes) {
                        super.onFail(mes);
                        refreshLayout.setRefreshing(false);

                    }
                });
    }
    private void setData(boolean isRefresh, List data) {
        final int size = data == null ? 0 : data.size();
        page++;

        if (isRefresh) {
            adapter.setNewData(data);
        } else {
            if (size > 0) {
                adapter.addData(data);
            }
        }
        if (size < count) {
            //第一页如果不够一页就不显示没有更多数据布局
            adapter.loadMoreEnd(isRefresh);
        } else {
            adapter.loadMoreComplete();
        }
    }
}
